# -*- coding:utf-8 -*-
from selenium import webdriver
import win32gui
import win32con
import win32clipboard as w
import time
# ========================= 信息-需修改 =========================
username = "#####"  # 账号
pwd = "#####"  # 密码
recv_qq = "你的QQ大号昵称"  # 接收反馈的QQ
loading_time = 10  # 预留页面加载时间10秒 , 视电脑配置修改

# ========================= url-无需修改 =========================
login_url = "https://ehall.jlu.edu.cn/jlu_portal/login"  # 登录url
submit_url = "https://ehall.jlu.edu.cn/infoplus/form/YJSMRDK/start"  # 研究生每日打卡url
remind_url = "https://ehall.jlu.edu.cn/taskcenter/workflow/done"  # 已完成事项url

# ========================= xpath-无需修改 =========================
uid_xpath = './/input[@name="username"]'  # 账号xpath
pwd_xpath = './/input[@name="password"]'  # 密码xpath
button_xpath = './/input[@name="login_submit"]'  # 登录按钮xpath
ss_xpath = './/input[@id="V1_CTRL44"]'
confirm_xpath = './/a[@class="command_button_content"]'  # 确认按钮xpath
ok_xpath = './/button[@class="dialog_button default fr"]'  # 好按钮xpath

# ========================= 驱动-无需修改 =========================
global driver
driver = webdriver.Chrome()  # 设置Chrome驱动


def web_init():
    driver.maximize_window()  # 最大化窗口
    driver.implicitly_wait(6)  # 隐式等待
    driver.get(login_url)  # 获取网页


def web_close():
    driver.quit()  # 关闭所有关联的窗口


def web_load():
    time.sleep(loading_time)  # 预留页面加载时间5秒


def auto_login(username_, pwd_, uid_xpath_, pwd_xpath_, button_xpath_):
    web_load()
    driver.find_element_by_xpath(uid_xpath_).clear()  # 清空已存在内容
    driver.find_element_by_xpath(uid_xpath_).send_keys(username_)  # 传入用户名
    web_load()
    driver.find_element_by_xpath(pwd_xpath_).clear()  # 清空已存在内容
    driver.find_element_by_xpath(pwd_xpath_).send_keys(pwd_)  # 传入密码
    web_load()
    driver.find_element_by_xpath(button_xpath_).click()  # 通过登录按钮并单击登录


def submit(submit_url_, ss_xpath_, confirm_xpath_, ok_xpath_):
    driver.get(submit_url_)  # 获取网页
    web_load()
    driver.find_element_by_xpath(ss_xpath_).click()  # 通过登录按钮并单击登录
    driver.find_element_by_xpath(confirm_xpath_).click()  # 确认填报
    web_load()
    driver.find_element_by_xpath(ok_xpath_).click()  # 好
    web_load()


def remind(remind_url_, recv_qq_):
    driver.get(remind_url_)  # 获取网页
    web_load()
    row = driver.find_elements_by_tag_name('tr')
    result_list = []
    for i in row:
        j = i.find_elements_by_tag_name('td')
        for item in j:
            text = item.text
            result_list.append(text)
    today = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    result_msg = today+'-'+result_list[0]+'-'+result_list[1]+'-'+result_list[2]+'-'+result_list[3]
    print(result_msg)
    send(recv_qq_, result_msg)  # 发送最近的一条打卡记录

    
def send(name, msg):
    # 打开剪贴板
    w.OpenClipboard()
    # 清空剪贴板
    w.EmptyClipboard()
    # 设置剪贴板内容
    w.SetClipboardData(win32con.CF_UNICODETEXT, msg)
    # 获取剪贴板内容
    date = w.GetClipboardData()
    # 关闭剪贴板
    w.CloseClipboard()
    # 获取qq窗口句柄
    handle = win32gui.FindWindow(None, name)
    if handle == 0:
        print('未找到窗口！')
    # 显示窗口
    win32gui.ShowWindow(handle, win32con.SW_SHOW)
    # 把剪切板内容粘贴到qq窗口
    win32gui.SendMessage(handle, win32con.WM_PASTE, 0, 0)
    # 按下后松开回车键，发送消息
    win32gui.SendMessage(handle, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
    win32gui.SendMessage(handle, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
    time.sleep(2)  # 延缓进程


if __name__ == '__main__':
    web_init()  # 驱动初始化
    auto_login(username, pwd, uid_xpath, pwd_xpath, button_xpath)  # 登录操作
    submit(submit_url, ss_xpath, confirm_xpath, ok_xpath) # 打卡操作
    remind(remind_url, recv_qq)  # 发送提醒信息
    web_close()  # 测试结束
